!Subroutine to compute the electoral rule function
SUBROUTINE probmayor(pmayorin,imunin)
  USE Commonvars
  IMPLICIT NONE

  INTEGER, INTENT(IN)  :: imunin
  REAL(8), INTENT(OUT) :: pmayorin(Npast,Nq,Tend,Nrcont,Nsx,0:Nnterms)
  INTEGER              :: ipast, el_ap, iq, iage, isx
  REAL(8)              :: meandr, pnorm, pop_d3, dum_steal, dum_steal_x, qconsvec(Nq),dum_aud
 
  pop_d3 = 0d0
  IF (imunin == 3) pop_d3 = 1d0
                 
  !qconsveci is the log of the level of qcons
  IF (imunin == 1) THEN
     qconsvec = qconsvec1
  ELSE IF (imunin == 2) THEN
     qconsvec = qconsvec2
  ELSE IF (imunin == 3) THEN
     qconsvec = qconsvec3
  END IF

  DO ipast = 1, Npast
     DO iq = 1, Nq
        DO iage = 1, Tend
           DO el_ap = 1, Nelap
              DO isx = 1, Nsx
                 
                 dum_aud = 0.0d0
                 dum_steal = 0.0d0
                 IF(ipast == 1) THEN
                    dum_aud = 0.0d0
                    dum_steal = 0.0d0
                 ELSE IF(ipast == 2) THEN
                    dum_aud = 1.0d0
                    dum_steal = 0.0d0
                 ELSE IF(ipast == 3) THEN
                    dum_aud = 1.0d0
                    dum_steal = 1.0d0
                 END IF

                 dum_steal_x = 0d0                 
                 IF (isx ==2) THEN
                    dum_steal_x = 1d0
                 END IF
                                  
                 ! New ELECTORAL RULE
                 ! If low electoral appeal, then rcont = 0d0 and no bust in votes
                 ! If high electoral appeal, then rcont = 1d0 and increase in votes that corresponds to the parameter alpha(6)
                 ! The parameter on population size is equal to -alpha(4) time the rivalry parameter
                 meandr = alpha(1)+alpha(2)*dum_aud+alpha(3)*dum_aud*dum_steal+alpha(4)*qconsvec(iq) + alpha(5)*DBLE(iage) + alpha(6)*elap_vec(el_ap) - alpha(4)*eta*LOG(popsize(imunin)) + alpha(7)*pop_d3
                 
                 pmayorin(ipast,iq,iage,el_ap,isx,:) = pnorm(meandr/alpha(8))
                 
                 IF (pmayorin(ipast,iq,iage,el_ap,isx,1) < 0.0d0 .OR. pmayorin(ipast,iq,iage,el_ap,isx,1) > 1.0d0) write(*,*) 'Wrong pmayor', pmayorin(ipast,iq,iage,el_ap,isx,:)

                 ! If we want to implement a policy in which the mayor looses the election with probability 1 if he steals (equivalent to not allowing the mayor to run for reelection)
                 IF (flag_no_run == 1) THEN
                    IF (dum_aud*dum_steal > 0.5d0) pmayorin(ipast,iq,iage,el_ap,isx,:) = 0d0
                 END IF
                 ! If we want to implement a policy in which the mayor looses the election with probability 1 if he steals more than x (equivalent to not allowing the mayor to run for reelection)
                 IF (flag_no_run_x == 1) THEN
                    IF (dum_aud*dum_steal_x > 0.5d0) pmayorin(ipast,iq,iage,el_ap,isx,:) = 0d0
                 END IF

!!$                 if (myrank == 0) write(*,'(a,5i5,2f20.10)') 'pmayor', ipast,iq,iage,el_ap,isx,pnorm(meandr/alpha(9)), meandr/alpha(9)
!!$                 if (myrank == 0) write(*,'(a,13f14.7)') 'inputs', alpha(1), alpha(2)*dum_aud, alpha(3)*dum_aud*dum_steal, alpha(4)*dum_aud*dum_steal_x, alpha(5)*qconsvec(iq), alpha(6)*DBLE(iage), alpha(7)*rcont(irc), alpha(5)*eta*LOG(popsize(imunin)), alpha(8)*pop_d1, dum_aud, dum_steal, dum_steal_x

              END DO
           END DO
        END DO
     END DO
  END DO

  ! If the incumbent is currently in the last of the possible terms (Nnterms), then the prob of reelection = 0
  pmayorin(:,:,:,:,:,Nnterms) = 0d0
  ! The incumbent cannot produce in iterm == 1, he would be a challenger, then the prob of reelection = 0
  pmayorin(:,:,:,:,:,0) = 0d0

  ! If the incumbent's age is greater than or equal to Retage, the prob of reelection = 0
  DO iage = Retage, Tend
     pmayorin(:,:,iage,:,:,:) = 0d0
  END DO

END SUBROUTINE probmayor
